package com.huazai.apply.manager.web.action;

import com.huazai.apply.manager.bean.User;
import com.huazai.apply.manager.service.UserInitService;
import com.huazai.apply.manager.service.impl.UserInitServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author EZTTU
 */
@WebServlet(name = "door", value = {"/welcome", "/logout"})
public class DoorServlet extends HttpServlet {
    private static final String SERVICE_WELCOME = "/welcome";
    private static final String SERVICE_LOGOUT = "/logout";

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String servletPath = request.getServletPath();

        if (servletPath.equals(SERVICE_WELCOME)) {
            doWelcome(request, response);
        } else if (servletPath.equals(SERVICE_LOGOUT)) {
            doLogout(request, response);
        }

    }

    private void doLogout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        String contextPath = request.getContextPath();
        if (session != null) {
            session.removeAttribute("UserMessage");
            session.invalidate();
        }
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if ("userid".equals(name) || "password".equals(name) || "field".equals(name)) {
                    cookie.setMaxAge(0);
                    cookie.setPath(contextPath);
                    response.addCookie(cookie);
                }
            }
        }
        response.sendRedirect(contextPath);
    }

    private void doWelcome(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String userid = null;
        String password = null;
        String field = null;
        String contextPath = request.getContextPath();

        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if ("userid".equals(name)) {
                    userid = cookie.getValue();
                } else if ("password".equals(name)) {
                    password = cookie.getValue();
                } else if ("field".equals(name)) {
                    field = cookie.getValue();
                }
            }
        }

        if (userid != null && password != null && field != null) {

            UserInitService userInitService = new UserInitServiceImpl();
            User user = new User(userid, password, null, field);
            String mes = userInitService.doLogin(user);

            if ("登录成功".equals(mes)) {
                HttpSession session = request.getSession();
                session.setAttribute("UserMessage", user);

                if ("user".equals(field)) {
                    response.sendRedirect(contextPath + "/user");
                } else {
                    response.sendRedirect(contextPath + "/admin");
                }
            } else {
                response.setContentType("text/html;charset=UTF-8");
                PrintWriter pw = response.getWriter();
                pw.print("<h1 align=\"center\">" + mes + "</h1>");
                pw.print("<a align=\"center\" href=\"login.jsp\">点这里重新登录</a>");
            }

        } else {
            response.sendRedirect(contextPath + "/login.jsp");
        }
    }
}
